﻿using DevExpress.Web.ASPxTreeView;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDream.Page
{
    public partial class sodo : System.Web.UI.UserControl
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int month = DateTime.Now.Month;
                int year = DateTime.Now.Year;
                DateTime lastday = GetLastDayOfMonth(year, month);
                DateTime dtfrom = GetfirstDayOfMonth(year, month);
                dteFrom.Date = dtfrom;
                dteTo.Date = lastday;

                BinTreeView();
            }

        }
        public static DateTime GetLastDayOfMonth(int year, int month)
        {
            return new DateTime(year, month, DateTime.DaysInMonth(year, month));
        }

        public static DateTime GetfirstDayOfMonth(int year, int month)
        {
            return new DateTime(year, month, 1);
        }
        void BinTreeView()
        {
            int id = 0;
            try
            {

                if (Request.QueryString["k"] != null)
                    id = int.Parse(Request.QueryString["k"].ToString());
                if (Request.QueryString["n"] != null)
                    lb.Text = "Hệ thống của: " + Request.QueryString["n"];
            }
            catch (Exception) { }
            try
            {
                UserDB user_obj = (UserDB)Session[Global.SESS_USER];
                //DataClasses1DataContext dx = new DataClasses1DataContext();
                if (id == 0)
                    BindDataFromDB(user_obj.ID);
                else
                    BindDataFromDB(id);
            }
            catch (Exception) { }
        }

        private void AddChildNode(TreeViewNode parent, int menuId)
        {

            var query1 = from menu1 in context.Employees where menu1.ParentID == menuId select menu1;
            foreach (var item in query1)
            {
                TreeViewNode child = new TreeViewNode();
                child.Image.Url = "~/Image/icon/" + item.avata;
                child.Text = item.UserName + ": " + item.Lastname + " " + item.FirstName;
                child.Target = "_blank";
                if (item.DateBegin >= dteFrom.Date && item.DateBegin <= dteTo.Date)
                    child.TextStyle.ForeColor = Color.Blue;
                child.NavigateUrl = "~/Canhan.aspx?page=so-do&k=" + item.UserID + "&n=(" + item.UserName + ") " + item.Lastname + " " + item.FirstName;
                parent.Nodes.Add(child);

                AddChildNode(child, (int)item.UserID);
            }


        }

        private void BindDataFromDB(int parent)
        {
            context.Connection.Open();
            try
            {
                string[,] parentNode = new string[1000, 4];

                var query = from menu in context.Employees where menu.ParentID == parent select menu;
                int count = 0;

                foreach (var item in query)
                {
                    parentNode[count, 0] = item.UserID.ToString();
                    parentNode[count, 1] = item.avata;
                    parentNode[count, 2] = item.DateBegin.ToString();
                    parentNode[count++, 3] = item.UserName + ": " + item.Lastname + " " + item.FirstName;

                }

                for (int i = 0; i < count; i++)
                {
                    TreeViewNode root = new TreeViewNode();
                    root.Image.Url = "~/Image/icon/" + parentNode[i, 1];
                    root.Text = parentNode[i, 3];
                    root.Target = "_blank";
                    try
                    {
                        string d = dteFrom.Date.ToString();
                        string d1 = dteTo.Date.ToString();
                        DateTime nf = DateTime.Parse(d);
                        DateTime nt = DateTime.Parse(d1);
                        DateTime dteBegin = DateTime.Parse(parentNode[i, 2]);
                        if (dteBegin >= nf && dteBegin <= nt)
                            root.TextStyle.ForeColor = Color.Blue;
                    }
                    catch (Exception) { }
                    root.NavigateUrl = "~/Canhan.aspx?page=so-do&k=" + parentNode[i, 0] + "&n=(" + parentNode[i, 3].Substring(0, parentNode[i, 3].IndexOf(":")) + ")" + parentNode[i, 3].Substring(parentNode[i, 3].IndexOf(":") + 1);

                    ASPxTreeView1.Nodes.Add(root);

                    var query1 = from menu1 in context.Employees where menu1.ParentID.ToString() == parentNode[i, 0] select menu1;

                    foreach (var item in query1)
                    {
                        TreeViewNode child = new TreeViewNode();
                        child.Image.Url = "~/Image/icon/" + item.avata;
                        child.Text = item.UserName + ": " + item.Lastname + " " + item.FirstName;
                        child.Target = "_blank";
                        if (item.DateBegin >= dteFrom.Date && item.DateBegin <= dteTo.Date)
                            child.TextStyle.ForeColor = Color.Blue;
                        child.NavigateUrl = "~/Canhan.aspx?page=so-do&k=" + item.UserID + "&n=(" + item.UserName + ") " + item.Lastname + " " + item.FirstName;

                        root.Nodes.Add(child);
                        AddChildNode(child, (int)item.UserID);

                    }


                }

                ASPxTreeView1.ExpandAll();
            }
            catch (Exception) { }
            finally { context.Connection.Close(); }
            //treeview.CollapseAll(); 
        }

        protected void ASPxCallbackPanel1_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            ASPxTreeView1.Nodes.Clear();
            BinTreeView();
        }

        protected void btnTim_Click(object sender, EventArgs e)
        {
            ASPxTreeView1.Nodes.Clear();
            BinTreeView();
        }
    }
}